package com.android.phoenix.activity.location;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.phoenix.R;
import com.android.phoenix.activity.BaseActivity;
import com.baidu.mapapi.map.offline.MKOLSearchRecord;
import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.baidu.mapapi.map.offline.MKOfflineMap;
import com.baidu.mapapi.map.offline.MKOfflineMapListener;

public class OfflineMapActivity extends BaseActivity implements
		OnClickListener, MKOfflineMapListener {
	private ImageView title_Imgbtn_left_up;
	private TextView title_txt_center;
	private TextView title_txt_right_up;

	private EditText edit_offline_searchcity;
	private ImageView img_offline_searchcity;
	private TextView text_offline_city;
	private TextView text_offline_size;
	private Button btn_offline_start;
	private Button btn_offline_pause;
	private ListView list_offline_downloaded;

	private MKOfflineMap mOffline = null;
	private LocalMapAdapter localAdapter = null;
	private int cityid = 0;
	/**
	 * 已下载的离线地图信息列表
	 */
	private ArrayList<MKOLUpdateElement> localMapList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_offline_map);
		initTitleBar();
		mOffline = new MKOfflineMap();
		mOffline.init(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initView();
		initOfflineMap();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.title_Imgbtn_left_up:
			finish();
			break;
		case R.id.img_offline_searchcity:
			search();
			break;
		case R.id.btn_offline_start:
			start();
			break;
		case R.id.btn_offline_pause:
			stop();
			break;
		}
	}

	private void initView() {
		edit_offline_searchcity = (EditText) this
				.findViewById(R.id.edit_offline_searchcity);
		img_offline_searchcity = (ImageView) this
				.findViewById(R.id.img_offline_searchcity);
		text_offline_city = (TextView) this
				.findViewById(R.id.text_offline_city);
		text_offline_size = (TextView) this
				.findViewById(R.id.text_offline_size);
		btn_offline_start = (Button) this.findViewById(R.id.btn_offline_start);
		btn_offline_pause = (Button) this.findViewById(R.id.btn_offline_pause);
		list_offline_downloaded = (ListView) this
				.findViewById(R.id.list_offline_downloaded);
		img_offline_searchcity.setOnClickListener(this);
		btn_offline_start.setOnClickListener(this);
		btn_offline_pause.setOnClickListener(this);

	}

	private void initTitleBar() {
		title_Imgbtn_left_up = (ImageView) this
				.findViewById(R.id.title_Imgbtn_left_up);
		title_Imgbtn_left_up.setOnClickListener(this);
		title_txt_center = (TextView) this.findViewById(R.id.title_txt_center);
		title_txt_center.setText(getResources().getString(
				R.string.txt_offline_titlebarcenter));
		title_txt_right_up = (TextView) this
				.findViewById(R.id.title_txt_right_up);
		title_txt_right_up.setVisibility(View.GONE);
	}

	private void initOfflineMap() {
		// 获取已下过的离线地图信息
		localMapList = mOffline.getAllUpdateInfo();
		if (localMapList == null) {
			localMapList = new ArrayList<MKOLUpdateElement>();
		}
		localAdapter = new LocalMapAdapter();
		list_offline_downloaded.setAdapter(localAdapter);
	}

	/**
	 * 更新状态显示
	 */
	public void updateView() {
		localMapList = mOffline.getAllUpdateInfo();
		if (localMapList == null) {
			localMapList = new ArrayList<MKOLUpdateElement>();
		}
		localAdapter.notifyDataSetChanged();
	}

	/**
	 * 离线地图管理列表适配器
	 */
	public class LocalMapAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return localMapList.size();
		}

		@Override
		public Object getItem(int index) {
			return localMapList.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int index, View view, ViewGroup arg2) {
			MKOLUpdateElement e = (MKOLUpdateElement) getItem(index);
			view = View.inflate(OfflineMapActivity.this,
					R.layout.list_item_offline_localmap, null);
			initViewItem(view, e);
			return view;
		}

		void initViewItem(View view, final MKOLUpdateElement e) {
			TextView display = (TextView) view.findViewById(R.id.display);
			TextView remove = (TextView) view.findViewById(R.id.remove);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView update = (TextView) view.findViewById(R.id.update);
			TextView ratio = (TextView) view.findViewById(R.id.ratio);
			ratio.setText(e.ratio + "%");
			title.setText(e.cityName);
			if (e.update) {
				update.setText("可更新");
			} else {
				update.setText("最新");
			}
			if (e.ratio != 100) {
				display.setEnabled(false);
			} else {
				display.setEnabled(true);
			}
			remove.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					mOffline.remove(e.cityID);
					updateView();
				}
			});
			display.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.putExtra("x", e.geoPt.longitude);
					intent.putExtra("y", e.geoPt.latitude);
					intent.setClass(OfflineMapActivity.this, BaseMapDemo.class);
					startActivity(intent);
				}
			});
		}

	}

	@Override
	public void onGetOfflineMapState(int type, int state) {
		// TODO Auto-generated method stub
		switch (type) {
		case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
			MKOLUpdateElement update = mOffline.getUpdateInfo(state);
			// 处理下载进度更新提示
			if (update != null) {
				updateView();
			}
		}
			break;
		case MKOfflineMap.TYPE_NEW_OFFLINE:
			// 有新离线地图安装

			break;
		case MKOfflineMap.TYPE_VER_UPDATE:
			// 版本更新提示
			// MKOLUpdateElement e = mOffline.getUpdateInfo(state);

			break;
		}

	}

	/**
	 * 搜索离线需市
	 * 
	 * @param view
	 */
	private void search() {
		ArrayList<MKOLSearchRecord> records = mOffline
				.searchCity(edit_offline_searchcity.getText().toString());
		if (records == null || records.size() != 1)
			return;
		cityid = records.get(0).cityID;
		text_offline_city.setText(records.get(0).cityName);
		text_offline_size.setText(formatDataSize(records.get(0).size));

	}

	/**
	 * 开始下载
	 * 
	 * @param view
	 */
	private void start() {
		if (cityid == 0) {
			Toast.makeText(OfflineMapActivity.this, "请重新检索城市！！",
					Toast.LENGTH_LONG).show();
			return;
		}
		mOffline.start(cityid);
		Toast.makeText(this, "开始下载离线地图. cityid: " + cityid, Toast.LENGTH_SHORT)
				.show();
		updateView();
	}

	/**
	 * 暂停下载
	 * 
	 * @param view
	 */
	private void stop() {
		if (cityid == 0) {
			Toast.makeText(OfflineMapActivity.this, "出错，请检查是否有下载！！",
					Toast.LENGTH_LONG).show();
			return;
		}
		mOffline.pause(cityid);
		Toast.makeText(this, "暂停下载离线地图. cityid: " + cityid, Toast.LENGTH_SHORT)
				.show();
		updateView();
	}

	/**
	 * 删除离线地图
	 * 
	 * @param view
	 */
	public void remove(View view) {
		if (cityid == 0) {
			Toast.makeText(OfflineMapActivity.this, "出错，请检查是否有离线地图！！",
					Toast.LENGTH_SHORT).show();
			return;
		}
		mOffline.remove(cityid);
		Toast.makeText(this, "删除离线地图. cityid: " + cityid, Toast.LENGTH_SHORT)
				.show();
		updateView();
	}

	public String formatDataSize(int size) {
		String ret = "";
		if (size < (1024 * 1024)) {
			ret = String.format("%dK", size / 1024);
		} else {
			ret = String.format("%.1fM", size / (1024 * 1024.0));
		}
		return ret;
	}

	@Override
	protected void onPause() {
		if (cityid != 0) {
			MKOLUpdateElement temp = mOffline.getUpdateInfo(cityid);
			if (temp != null && temp.status == MKOLUpdateElement.DOWNLOADING) {
				mOffline.pause(cityid);
			}
		}
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		/**
		 * 退出时，销毁离线地图模块
		 */
		mOffline.destroy();
		super.onDestroy();
	}

}
